<?php

class Product extends Controller {
	
	function Product()
	{
		parent::Controller();
		$this->load->model('District_model');	
		$this->load->model('Country_model');	
		$this->load->model('City_model');	
		$this->load->model('Category_model');	
		$this->load->model('Product_model');	
		$this->lang->load('global_admin','english');
			
	}
	
	function index() {
		$array_where = array();
		$data['inc_script'] = '';
		$per_page = '10';
		$paging = array(
			'per_page'=>$per_page,
			'limit'   =>($this->uri->segment(3)=='')?'0':$this->uri->segment(3),
		);
		$data['product_info'] = $this->Product_model->getProducts($array_where, $paging);
		$this->load->view('administrator/product_view',$data);
	}	
	
	function add()
	{
		//get value from form
		$Product_info = array(
			'address' => $this->input->post('tf_addr'),
			'country_id' => $this->input->post('sl_country_id'),
			'city_id' => $this->input->post('sl_city_id'),
			'district_id' => $this->input->post('sl_dist_id'),
			'price' => $this->input->post('tf_price'),
			'phone' => $this->input->post('tf_phone'),
			'geo_location' => $this->input->post('tf_location'),
			'date_begin' => $this->input->post('tf_dtbe'),
			'date_end' => $this->input->post('tf_dted'),
			'quantity' => $this->input->post('tf_quat'),
		);
		
		$form_validation = array(
			array('field' => 'tf_addr','label' => 'lang:address','rules' => 'required|max_length[45]'),
		);
		$this->form_validation->set_rules($form_validation);

		if ($this->form_validation->run() == true) {
			$this->Product_model->addProduct($Product_info);
			redirect('administrator/Product');			
		}
		
		$data['inc_script'] = '';
		$data['country_info'] = $this->Country_model->getCountries();
		$data['city_info'] = $this->City_model->getCities();
		$data['district_info'] = $this->District_model->getDistricts();
		$this->load->view('administrator/product_add_view', $data);
	}
	
	function addproimg()
	{
		//get District id from uri
		$id = $this->uri->segment(4);
	}
	
	function addprodetail()
	{
		//get District id from uri
		$id = $this->uri->segment(4);
	}
	
	function edit()
	{
		//get District id from uri
		$id = $this->uri->segment(4);
		
		//get value from form
		$Product_info = array(
			'address' => $this->input->post('tf_addr'),
			'country_id' => $this->input->post('sl_country_id'),
			'city_id' => $this->input->post('sl_city_id'),
			'district_id' => $this->input->post('sl_dist_id'),
			'price' => $this->input->post('tf_price'),
			'phone' => $this->input->post('tf_phone'),
			'geo_location' => $this->input->post('tf_location'),
			'date_begin' => $this->input->post('tf_dtbe'),
			'date_end' => $this->input->post('tf_dted'),
			'quantity' => $this->input->post('tf_quat'),
		);
		
		$form_validation = array(
			array('field' => 'tf_addr','label' => 'lang:address','rules' => 'required|max_length[45]'),
		);
		$this->form_validation->set_rules($form_validation);

		if ($this->form_validation->run() == true) {
			$this->Product_model->updateProduct($id,$Product_info);
			redirect('administrator/Product');			
		}
		
		$data['form_info'] = $this->Product_model->getProduct2(array('id'=>$id));
		$data['inc_script'] = '';
		$data['country_info'] = $this->Country_model->getCountries();
		$data['city_info'] = $this->City_model->getCities();
		$data['district_info'] = $this->District_model->getDistricts();
		$this->load->view('administrator/product_edit_view', $data);
	}
	
	function delete()
	{
		$id = $this->uri->segment(4);	
		$this->Product_model->deleteProduct($id);
		redirect('administrator/Product');		
	}
}